﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Lephone.Data.SqlEntry;
using Lephone.Data;
using CsharpTpl.common;
using CsharpTpl.model;

namespace CsharpTpl.window
{
    public partial class FrmCZYManage : Form
    {
        private long czyID;
        public FrmCZYManage()
        {
            InitializeComponent();
            CZYListView.SelectionMode = DataGridViewSelectionMode.FullRowSelect;
            CZYListView.AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.Fill;
            CZYListView.EditMode = DataGridViewEditMode.EditProgrammatically;
            CZYListView.AllowUserToAddRows = false;
            this.Width = 465;
            btnAdd.Click += new EventHandler(btnAdd_Click);
            btnQD.Click += new EventHandler(btnSave_Click);
            btnQX.Click += new EventHandler(btnQX_Click);
            CZYListView.SelectionChanged += new EventHandler(CZYListView_SelectionChanged);
            btnModify.Click += new EventHandler(btnAdd_Click);
            btnDelete.Click += new EventHandler(btnDelete_Click);
            btnJSFP.Click +=new EventHandler(btnJSFP_Click);
            LoadCZYList();
            CZYListView.Columns[2].Visible = false;
        }
        void btnJSFP_Click(object sender,EventArgs e)
        {
            FrmRoleList frmRoleList = new FrmRoleList(czyID);
            frmRoleList.Show(this);
        }
        void LoadCZYList()
        {
            SqlStatement sql = new SqlStatement("select ID 操作员ID,MC 操作员名称, MM 密码,DH 用户代号 from T_CZY");
            DataSet ds = DbEntry.Context.ExecuteDataset(sql);
            CZYListView.DataSource = ds.Tables[0];
        }
        void btnAdd_Click(object sender, EventArgs e)
        {
            if (this.Width < 500)
            {
                this.Width = 715;
            }
            ToolStripButton btn = (ToolStripButton)sender;
            if (btn.Text.Equals("新增操作员"))
            {
                groupBox1.Text = "新增操作员";
                tBoxMC.Clear();
                tBoxMM.Clear();
                tBoxDH.Clear();
                btnQD.Text = "保存";
            }
            else
            {
                groupBox1.Text = "修改操作员";
                btnQD.Text = "更新角色";
                tBoxMC.Text = CZYListView.SelectedRows[0].Cells[1].Value.ToString();
                tBoxMM.Text = CZYListView.SelectedRows[0].Cells[2].Value.ToString();
                tBoxDH.Text = CZYListView.SelectedRows[0].Cells[3].Value.ToString();
                tBoxDH.ReadOnly = true;
                CZYListView.Enabled = false;
            }
            tBoxMC.Focus();
        }
        void btnSave_Click(object sender, EventArgs e)
        {
            if (tBoxMC.Text!=null&&tBoxMM!=null&&tBoxDH!=null)
            {
                if (btnQD.Text == "保存")
                {
                    T_CZY czy = T_CZY.New;
                    czy.MC = tBoxMC.Text.Trim();
                    czy.MM = tBoxMM.Text.Trim();
                    czy.DH = tBoxDH.Text.Trim();
                    if (T_CZY.FindOne(CK.K["DH"]==tBoxDH.Text.Trim())!=null)
                    {
                        MessageBox.Show("操作员代号重复！");
                        tBoxDH.Focus();
                        return;
                    }
                    czy.Save();
                }
                else
                {
                    T_CZY czy = T_CZY.New;
                    czy.Id = czyID;
                    czy.MC = tBoxMC.Text.Trim();
                    czy.MM = tBoxMM.Text.Trim();
                    czy.DH = tBoxDH.Text.Trim();
                    czy.Save();
                }
                LoadCZYList();
                btnQX.PerformClick();
            }

        }
        void btnQX_Click(object sender, EventArgs e)
        {
            this.Width = 465;
            CZYListView.Enabled = true;
        }
        void CZYListView_SelectionChanged(object sender, EventArgs e)
        {
            if (CZYListView.SelectedRows.Count > 0)
            {
                czyID = long.Parse(CZYListView.SelectedRows[0].Cells[0].Value.ToString());
                btnModify.Enabled = true;
                btnDelete.Enabled = true;
            }
            else
            {
                btnModify.Enabled = false;
                btnDelete.Enabled = false;
            }
        }
        void btnDelete_Click(object sender, EventArgs e)
        {
            if (CZYListView.SelectedRows.Count>0)
            {
                if (MessageBox.Show(this, "确认要删除此项记录么?此操作无法恢复.", "警告", MessageBoxButtons.YesNo) == DialogResult.Yes )
                {
                    DbEntry.Delete(T_CZY.FindById(czyID));
                    DbEntry.Context.ExecuteNonQuery("delete from t_czyjs where CZYID=" + czyID);
                    LoadCZYList();
                }
            }
        }
        
        }
    }
